<template>
  <div>
    <el-card shadow="hover" class="mt-20">
      <el-form>
        <el-form-item label="属性名" class="attrList-input">
          <el-input
            placeholder="请输入属性名"
            v-model="attrFormData.attrName"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-form>
        <el-button
          type="primary"
          :disabled="!attrFormData.attrName"
          :icon="Plus"
          @click="addAttrValue"
          >添加属性值
        </el-button>
        <el-button @click="cancel">取消</el-button>
        <el-table :data="attrFormData.attrValueList" class="mt-20" border>
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="100px"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template v-slot="{ row, $index }">
              <el-input
                v-if="row.isShowEdit"
                size="small"
                ref="inputRef"
                @blur="switchEdit(row, $index)"
                v-model="row.valueName"
              />
              <div v-else @click="switchShow(row)">
                {{ row.valueName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-popconfirm
                :title="`您确定要删除 ${row.valueName} 这条信息吗?`"
                @confirm="deleValue($index)"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    :icon="Delete"
                    size="small"
                    title="删除"
                  />
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <el-form-item class="mt-20">
        <el-button
          type="primary"
          :disabled="isBtnDisabled"
          @click="addOrUpdateAttrList"
          >保存</el-button
        >
        <el-button>取消</el-button>
      </el-form-item>
    </el-card>
  </div>
</template>

<script lang="ts">
export default {
  name: "AddAttrList",
};
</script>

<script lang="ts" setup>
import { reactive, ref, nextTick, computed } from "vue";
import { Plus, Delete } from "@element-plus/icons-vue";
import type { AttrItem, AttrValueItem } from "@/api/product/model/attrModel";
import { postAttrInfoListApi } from "@/api/product/attr";
import { useCategoryStore } from "@/stores/category";
import { useAttrStore } from "@/stores/attr";
import { ElMessage } from "element-plus";

//获取input实例
const inputRef = ref();
// const isShowEdit = ref(true);

const categoryStore = useCategoryStore();
const attrStore = useAttrStore();

//获取表单数据
const attrFormData = reactive<AttrItem>({
  // 表单数据名称看接口文档
  attrName: attrStore.attrItem.attrName,
  attrValueList: attrStore.attrItem.attrValueList,
});

//点击添加属性值按钮
const addAttrValue = async () => {
  attrFormData.attrValueList.push({
    isShowEdit: true,
    valueName: "",
  });
  await nextTick();
  inputRef.value.focus();
};

//点击,点击input时,触发函数，切换到编辑模式
const switchShow = async (row: AttrValueItem) => {
  row.isShowEdit = true;
  //获取光标
  await nextTick();
  inputRef.value.focus();
};

//失去光标,当input失去光标时触发函数，切换到文本模式
const switchEdit = (row: AttrValueItem, index: number) => {
  if (!row.valueName) {
    attrFormData.attrValueList.splice(index, 1);
  }
  row.isShowEdit = false;
};

//删除信息
const deleValue = (index: number) => {
  attrFormData.attrValueList.splice(index, 1);
};

const isBtnDisabled = computed(() => {
  // 属性名有值 并且 属性值列表至少有一个属性值名称
  return !(
    attrFormData.attrName &&
    attrFormData.attrValueList.some((attrValue) => attrValue.valueName)
  );
});

// 声明接收传递过来的自定义函数
const emit = defineEmits(["setIsShowAttrList"]);
const cancel = () => {
  emit("setIsShowAttrList", true);
};

//点击保存,发送请求
const addOrUpdateAttrList = async () => {
  const { attrName, attrValueList } = attrFormData;
  const { category3Id } = categoryStore;
  const { id } = attrStore.attrItem;
  await postAttrInfoListApi({
    attrName,
    attrValueList,
    categoryLevel: 3,
    categoryId: category3Id,
    id,
  });
  ElMessage.success("添加属性成功");
  cancel();
};
</script>

<style scoped>
.attrList-input {
  width: 250px;
}
</style>
